package com.common;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HbmBaseDao extends HibernateDaoSupport
{
	/**
	 * 
	 * @param entity
	 */
	public void delete(Object entity)
	{
		this.getHibernateTemplate().delete(entity);
	}

	/**
	 * 
	 * @param entities
	 */
	public void deleteAll(Collection entities)
	{
		this.getHibernateTemplate().deleteAll(entities);
	}
	
	/**
	 * 
	 * @return void
	 */
	public void deleteHql(String hql)
	{
		this.getHibernateTemplate().delete(hql);
	}
	
	/**
	 * 
	 * @return list
	 */
	public List find(String hql,Object[] queryObject)
	{
		List list=null;
		list=this.getHibernateTemplate().find(hql,queryObject);
		return list;
	}
	
	/**
	 * 
	 * @param hql
	 * @return
	 */
	public List find(String hql)
	{
		List list=null;
		list=this.getHibernateTemplate().find(hql);
		return list;
	}
	
	

	public List getListForPage(final String hql, final int offset,final int length)
	{
		List list=getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException
			{
				Query query = session.createQuery(hql);
				query.setFirstResult(offset);
				query.setMaxResults(length);
				List list = query.list();
				return list;
			}
		});
	return list;
	}
	
	/**
	 * 
	 * @param entity
	 */
	public void save(Object entity)
	{
		this.getHibernateTemplate().save(entity);
	}
	
	/**
	 *
	 */
	public void update(Object entity)
	{
		this.getHibernateTemplate().update(entity);
	}
	
	/**
	 * 
	 */
	public void saveOrUpdate(Object entity)
	{
		this.getHibernateTemplate().saveOrUpdate(entity);
	}
	
}
